package com.wica.view;

// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnKeyListener;
import android.widget.Button;

import com.wica.facetube.R;

public class MoodDialog extends Activity implements OnKeyListener{
    protected static final String LOG_TAG = "_FT.MoodDialog__";
	private NotificationManager notificationManager;
	private Button button;

    @Override
	protected void onCreate(Bundle savedInstanceState) {   	
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_LEFT_ICON);

        // Inflate our UI from its XML layout description.
        setContentView(R.layout.mood_dialog);

        // Get display items for later interaction
        button = (Button) findViewById(R.id.happyMarquee);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setMood(R.drawable.stat_happy, R.string.status_bar_notifications_happy_message,
                        true);
            }
        });
        button = (Button) findViewById(R.id.neutralMarquee);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setMood(R.drawable.stat_neutral, R.string.status_bar_notifications_ok_message, true);
            }
        });
        button = (Button) findViewById(R.id.sadMarquee);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setMood(R.drawable.stat_sad, R.string.status_bar_notifications_sad_message, true);
            }
        });
        button = (Button) findViewById(R.id.angryMarquee);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setMood(R.drawable.stat_angry, R.string.status_bar_notifications_angry_message, true);
            }
        });
        
        // Get the notification manager service.
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    }// onCreate method
    
	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			return true;
  	  	}else {
  	  		return super.onKeyDown(keyCode, event);
  	  	}
	}// onKey method
	
    private void setMood(int moodId, int textId, boolean showTicker) {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(textId);
        String moodString="";

        // choose the ticker text
        String tickerText = showTicker ? getString(textId) : null;

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(moodId, tickerText, System.currentTimeMillis());
        
        // User is happy. Vibration!!
        if (moodId==R.drawable.stat_happy)
            notification.defaults = Notification.DEFAULT_VIBRATE;

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MoodDialog.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.status_bar_notifications_mood_title),
                       text, contentIntent);

        // Send the notification.
        // We use a layout id because it is a unique number.  We use it later to cancel.
        notificationManager.notify(R.layout.status_selection, notification);
        
        //We set the preference mood
        Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
        switch (moodId){
        	case R.drawable.stat_happy:
        		moodString="Happy";
        		break;
        	case R.drawable.stat_neutral:
        		moodString="Neutral";
        		break;
        	case R.drawable.stat_sad:
        		moodString="Sad";
        		break;
        	case R.drawable.stat_angry:
        		moodString="Angry";
        		break;
        }        
		editor.putString("mood", moodString);
		editor.commit();
		if (getCallingActivity()!=null){
			Intent intent = new Intent(MoodDialog.this, (getCallingActivity().getClass()));
	    	setResult(RESULT_OK,intent);
		}else{
			Intent intent = new Intent(MoodDialog.this, ListOfMoviesTabs.class);
	    	setResult(RESULT_OK,intent);
		}
        finish();
    }
}